package com.hp.bon.sgw.wh.channel.especial.bizSlot;

import com.alibaba.fastjson.JSONObject;
import com.hp.bon.sgw.bean.XmlMessage;
import com.hp.bon.sgw.util.Comutils;
import com.hp.bon.sgw.wh.channel.common.BizConsts;
import com.hp.bon.sgw.wh.channel.common.BizParam;
import com.hp.bon.sgw.wh.channel.common.BizResult;
import com.hp.bon.sgw.wh.channel.outside.BizSlot;
import com.hp.bon.sgw.wh.channel.outside.bizSlotOutQuery.utils.HttpClientUtils;
import org.dom4j.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;

public class JfzsRechargeInfoSaveBizSlot implements BizSlot {
    private static final Logger _log = LoggerFactory.getLogger(JfzsRechargeInfoSaveBizSlot.class);

    @Override
    public BizResult doBiz(XmlMessage xmlMessage, BizParam bizParam) {
        BizResult bizResult = new BizResult();
        try {

            String staffId = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/Staff_Id");
            String productNo = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/PRODUCTNO");
            String txnAmount = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/TXNAMOUNT");
            String rechargeAmt = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/RECHARGEAMT");
            String terminalSeqno = HttpClientUtils.getXmlValue(xmlMessage, "/Service_Information/TERMINALSEQNO");



            JSONObject operAttrStruct = new JSONObject();
            operAttrStruct.put("operServiceId", "-1");
            operAttrStruct.put("lanId", "8420100");
            operAttrStruct.put("operPost", "0");
            operAttrStruct.put("operOrgId", "0");
            operAttrStruct.put("staffId", staffId);
            operAttrStruct.put("operTime", "0");

            JSONObject svcObjectStruct = new JSONObject();
            svcObjectStruct.put("objValue", productNo);
            svcObjectStruct.put("objAttr", "0");
            svcObjectStruct.put("objType", "3");
            svcObjectStruct.put("dataArea", "0");
            com.alibaba.fastjson.JSONObject jsonObject = new com.alibaba.fastjson.JSONObject();
            jsonObject.put("operAttrStruct", operAttrStruct);
            jsonObject.put("svcObjectStruct", svcObjectStruct);
            jsonObject.put("accNbr", productNo);
            //"blockDate":"027",   --取入参ACCEPTTIME  ???? 没有这个入参
            jsonObject.put("blockDate", "");
            jsonObject.put("balanceAmount", txnAmount);
            jsonObject.put("reverseAmount", rechargeAmt);
            jsonObject.put("operType", "1");
            jsonObject.put("operSerialNbr", terminalSeqno);
            jsonObject.put("operSerial", "");

            StringBuilder buildStringMes = new StringBuilder();
            buildStringMes.append(jsonObject.toJSONString());

            //发送请求
            String webserviceUrl = bizParam.getParamString(BizConsts.KEY_ACCOUNT_API_URL);
            String respStr = HttpClientUtils.postHttpStr(buildStringMes.toString(), webserviceUrl);

            if (respStr.equals("")) {
                bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
                return bizResult;
            }

            bizResult.setHttpBody(respStr);
        } catch (Exception e) {
            bizResult.setCode(BizConsts.STATUS_ACOUNT_API_FAIL);
            _log.info(e.toString());
        }
        return bizResult;
    }

    public static void jsonToXmlStr(BizParam bizParam, StringBuffer buffer, String accNbr, List<String> list, String startTime, String endTime) {
        buffer.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
        buffer.append("<HqCheck>");
        buffer.append("<BillingMode>1</ BillingMode >");
        buffer.append("<MDN>" + accNbr + "</MDN>");
        buffer.append("<ServId>" + bizParam.getParamString("prodInstId") + "</ServId>");
        buffer.append("<AreaCode>" + bizParam.getParamString("regionId") + "</AreaCode>");
        buffer.append("<CkMDNlist>");
        for (int i = 0; i < list.size(); i++) {
            buffer.append("<CkMDN>" + list.get(i) + "</CkMDN>");
        }
        buffer.append("</CkMDNlist>");
        buffer.append("<StartTime>" + startTime + "</StartTime>");
        buffer.append("<EndTime>" + endTime + "</EndTime>");
        buffer.append("</HqCheck>");
    }

}
